package com.valueanimator;

import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private Button btnHello;

    private Button btnAlpha;
    private Button btnScale;
    private Button btnRotate;
    private Button btnTranslate;
    private Button btnSet;

    private Button btnCustomize;
    private MyButton myButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bindView();
        ClickEvent();
    }

    private void ClickEvent() {
        btnAlpha.setOnClickListener(v -> alphaAnimator());
        btnScale.setOnClickListener(v -> scaleAnimator());
        btnRotate.setOnClickListener(v -> rotateAnimator());
        btnTranslate.setOnClickListener(v -> translateAnimator());
        btnSet.setOnClickListener(v -> setAnimator());
        btnCustomize.setOnClickListener(v -> customizeAnimator());
    }

    private void customizeAnimator() {
        ObjectAnimator animator = ObjectAnimator.ofInt(myButton, "myText", 0, 360);
        animator.setDuration(3000);
        animator.setRepeatCount(3);
        animator.addUpdateListener(animation -> {
            int value = (int) animation.getAnimatedValue();
            myButton.setRotation(value);
        });
        animator.start();
    }

    private void setAnimator() {
        //动态加载
        /*float curTranslationX = btnHello.getTranslationX();
        //设置动画效果
        ObjectAnimator translation = ObjectAnimator.ofFloat(btnHello, "translationX", curTranslationX, 200, curTranslationX);
        ObjectAnimator rotation = ObjectAnimator.ofFloat(btnHello, "Rotation", 0, 360, 0);
        ObjectAnimator alpha = ObjectAnimator.ofFloat(btnHello, "alpha", 1, 0, 1);
        ObjectAnimator scale = ObjectAnimator.ofFloat(btnHello, "scaleX", 1, 2, 1);

        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.play(translation).with(rotation).before(alpha).before(scale);
        animatorSet.setDuration(3000);*/

        //xml加载
        AnimatorSet animatorSet = (AnimatorSet) AnimatorInflater.loadAnimator(MainActivity.this, R.animator.set_animator);
        animatorSet.setTarget(btnHello);

        animatorSet.start();
    }

    private void translateAnimator() {
        //动态加载
        /*ObjectAnimator animator = ObjectAnimator.ofFloat(btnHello, "TranslationX", 0, 200, 0);
        animator.setDuration(3000);*/

        //xml加载
        ObjectAnimator animator = (ObjectAnimator) AnimatorInflater.loadAnimator(MainActivity.this, R.animator.translate_animator);
        animator.setTarget(btnHello);

        animator.addUpdateListener(animation -> {
            float value = (float) animation.getAnimatedValue();
            btnHello.setTranslationY(value);
        });
        animator.start();
    }

    private void rotateAnimator() {
        //动态加载
       /* ObjectAnimator animator = ObjectAnimator.ofFloat(btnHello, "Rotation", 0, 360, 0);
        animator.setDuration(3000);*/

        //xml加载
        ObjectAnimator animator = (ObjectAnimator) AnimatorInflater.loadAnimator(MainActivity.this, R.animator.rotate_animator);
        animator.setTarget(btnHello);
        animator.start();
    }

    private void scaleAnimator() {
        //动态加载
        /*ObjectAnimator animator = ObjectAnimator.ofFloat(btnHello, "scaleX", 1, 2, 1);
        animator.setDuration(3000);*/

        //xml加载
        ObjectAnimator animator = (ObjectAnimator) AnimatorInflater.loadAnimator(MainActivity.this, R.animator.scale_animator);
        animator.setTarget(btnHello);
        animator.start();
    }

    private void alphaAnimator() {
        //动态加载
     /*   ObjectAnimator animator = ObjectAnimator.ofFloat(btnHello, "alpha", 1, 0, 1);
        animator.setDuration(3000);*/

        //xml加载
        ObjectAnimator animator = (ObjectAnimator) AnimatorInflater.loadAnimator(MainActivity.this, R.animator.alpha_animator);
        animator.setTarget(btnHello);
        animator.start();
    }

    private void bindView() {
        btnHello = findViewById(R.id.btn_Hello);
        btnAlpha = findViewById(R.id.btn_alpha);
        btnScale = findViewById(R.id.btn_scale);
        btnRotate = findViewById(R.id.btn_rotate);
        btnTranslate = findViewById(R.id.btn_translate);
        btnSet = findViewById(R.id.btn_set);

        btnCustomize = findViewById(R.id.btn_customize);
        myButton = findViewById(R.id.my_button);
    }
}
